<?php

namespace Home\Controller;

use Think\Controller;

/**
 * Description of 父类控制器实例
 * @author zhangzhen
 * @date 2017-01-03
 */
class DealerParentController extends \Common\Controller\BaseController {

    protected function _initialize() {
        parent::_initialize();
        $this->checkLogin();
        $this->checkOverTime();
        $this->assignUser();
        $this->checkLanguage();
    }

    /**
     * 根据语言切换web页面语言
     * @modify trainyao 2016-02-24    创建
     */
    protected function checkLanguage() {
        if (!in_array(LANG_SET, array('zh_cn', 'en_us', 'zh_tw'))) {
            $this->theme('en_us');
        } else {
            $this->theme(LANG_SET);
        }
    }

    /**
     * 判断是否登录，未登录跳转到登录页面
     * @modify zhangzhen 2017-01-03
     */
    protected function checkLogin() {
        $userId = getDealerId();
        // session和cookie都没有登录
        if (!$userId && !$this->checkRememberMe()) {
            if (IS_AJAX) {
                returnJson(403, 203, 'error');
            } else {
                redirect('/dealer.html');
            }
        }
    }

    /**
     * 信息页面跳转提示
     * @param type $message
     * @param type $jumpUrl
     * @param type $ajax
     */
    protected function messageTips($message = '', $jumpUrl = '', $ajax = false, $waitSecond) {
        if (true === $ajax || IS_AJAX) {
            $data = is_array($ajax) ? $ajax : array();
            $data['info'] = $message;
            $data['url'] = $jumpUrl;
            $this->ajaxReturn($data);
        }

        //保证输出不受静态缓存影响
        C('HTML_CACHE_ON', false);

        $this->assign('message', $message);

        // 提示信息
        // 成功操作后默认停留1秒
        if (!isset($waitSecond))
            $this->assign('waitSecond', '1');

        // 默认操作成功自动返回操作前页面
        if (!isset($jumpUrl))
            $this->assign("jumpUrl", $_SERVER["HTTP_REFERER"]);
        $this->display('MessagePage/dealertip');
        exit;
    }

    /**
     * 判断是否过期，已过期跳转到过期页面
     * @modify zhangzhen 2017-01-03
     */
    protected function checkOverTime() {
        $dealerId = getDealerId();
        $dealerLoginInfo = D('DealerLogin')->getInfo($dealerId);
        if($dealerLoginInfo['enddate'] < date('Y-m-d'))  
            $this->messageTips("经销商使用已过期,请联系客服");
    }

    /**
     * 判断是否登录，已登录跳转到房态页面
     * @modify zhangzhen 2017-01-03
     */
    protected function checkLogout() {
        $userId = getDealerId();
        // session或者cookie有登录
        if (!empty($userId) || $this->checkRememberMe()) {
            if (IS_AJAX) {
                returnJson(403, 202, 'error');
            } else {

                redirect('/Home/DealerPage/index.html');
            }
        }
    }

    /**
     * 判断是否记住登录，即拥有登录cookie，调用前提是没有登录session
     * @modify zhangzhen 2017-01-03
     */
    private function checkRememberMe() {
        // 该项用于唯一限制用户登录，暂时没有用处
        // $guid = cookie(C('COOKIE_SERVER_REMEMBER_ME_UUID'));
        $userSSID = cookie(C('COOKIE_SERVER_REMEMBER_DEALER_USSID'));
        if (empty($userSSID)) {
            return false;
        }

        $RsaEncrypt = new \Org\Util\RSAEncrypt();
        $userId = $RsaEncrypt->meizhuDecrypt($userSSID);
        if (empty($userId)) {
            return false;
        }

        $userSSPS = cookie(C('COOKIE_SERVER_REMEMBER_DEALER_USSPS'));
        $passportTime = cookie(C('COOKIE_SERVER_REMEMBER_DEALER_PSTM'));
        $dealerLoginLogic = A('Common/DealerLogin', 'Logic');
        $dealerLogin = $dealerLoginLogic->getInfo($userId);
       
        if (empty($dealerLogin)) {
            return false;
        }

        $expireTime = $passportTime + C('COOKIE_EXPIRE');
        if ($expireTime <= time()) {
            return false;
        }

        $compare = cookieDealerSSPS($dealerLogin['mobile'], $dealerLogin['password'], $passportTime);
        if ($userSSPS !== $compare) {
            cookie(C('COOKIE_SERVER_REMEMBER_DEALER_UUID'), null);
            cookie(C('COOKIE_SERVER_REMEMBER_DEALER_USSID'), null);
            cookie(C('COOKIE_SERVER_REMEMBER_DEALER_USSPS'), null);
            cookie(C('COOKIE_SERVER_REMEMBER_DEALER_PSTM'), null);
            return false;
        }

        //清除原有session数据，登录
        session(null);
        // 存储基本信息到session
        session(C('DEALER_AREACODE'), $dealerLogin['areacode']);
        session(C('DEALER_ID'), $dealerLogin['id']);
        session(C('DEALER_NAME'), $dealerLogin['name']);
        session(C('DEALER_MOBILE'), $dealerLogin['mobile']);
        session(C('DEALER_HEAD'), $dealerLogin['headpath']);
        return true;
    }

    /**
     * @description  获取用户登录状态
     * @modify zhangzhen 2017-01-03
     */
    protected function assignUser() {
        $this->assign('areaCode', session(C('DEALER_AREACODE')));
        $this->assign('userName', session(C('DEALER_NAME')));
        $this->assign('userMobile', session(C('DEALER_MOBILE')));
        $this->assign('userHead', session(C('DEALER_HEAD')));
    }

}
